const express=require('express');
//引入连接池模块
const pool=require('../pool.js');
//创建路由器对象
const r=express.Router();

//get 登录的路由
r.get("/v1/login/:uname&:upwd",(req,res)=>{
var _uname=req.params.uname;
var _upwd=req.params.upwd;
console.log(_uname,_upwd)
var sql="select * from xz_user where uname=? and upwd=?";
	pool.query(sql,[_uname,_upwd],(err,result)=>{
		if(err) throw err;
		if(result.length>0){
			res.send("1");
		}else{
			res.send("0");
		}
	});
});

//用户列表的路由
r.get("/V1/list",(req,res)=>{
	var sql="select * from xz_user";
	pool.query(sql,(err,result)=>{
		if(err)throw err;
		res.send(result);
	});
})

//删除用户的路由
r.delete("/v1/delete/:uid",(req,res)=>{
var _uid=req.params.uid;
var sql="delete from xz_user where uid=? ";
	pool.query(sql,[_uid],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows>0){
			res.send("1");
		}else{
			res.send("0");
		}
	});
});

//根据uid查找用户的路由
r.get("/v1/search/:uid",(req,res)=>{
	var _uid=req.params.uid;
var sql="sealect * from xz.user where uid=?";
	pool.query(sql,[_uid],(err,result)=>{
		if(err)throw err;
		res.send(result[0]);
	});
})

//根据uidx修改用户信息
r.put("/v1/update",(req,res)=>{
	var obj=req.body;
	var sql="update xz_user set ? where uid=?";
	pool.query(sql,[obj,obj.uid],(err,result)=>{
	if(err) throw err;
	if(result.affectedRows>0){
		res.send("1");
	}else{
		res.send("0");
	};
});
});
//导出路由器对象
module.exports=r;
 